package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.vo.SkuItemSaleAttrVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuSaleAttrValueDao;
import com.atguigu.gulimall.product.entity.SkuSaleAttrValueEntity;
import com.atguigu.gulimall.product.service.SkuSaleAttrValueService;


@Service("skuSaleAttrValueService")
public class SkuSaleAttrValueServiceImpl extends ServiceImpl<SkuSaleAttrValueDao, SkuSaleAttrValueEntity> implements SkuSaleAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuSaleAttrValueEntity> page = this.page(
                new Query<SkuSaleAttrValueEntity>().getPage(params),
                new QueryWrapper<SkuSaleAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询所有销售属性
     * @param spuId
     * @return
     */
    public List<SkuItemSaleAttrVo> getSaleAttrBySpuId(Long spuId) {
        /**
         SELECT
         ssav.attr_id,
         ssav.attr_name,
         GROUP_CONCAT(DISTINCT ssav.attr_value) attr_values
         FROM `pms_sku_info` info
         LEFT JOIN pms_sku_sale_attr_value ssav ON ssav.sku_id=info.sku_id
         WHERE info.spu_id=3
         GROUP BY ssav.attr_id,ssav.attr_name;
         */
        /**
         * SELECT
         * ssav.`attr_id` attr_id,
         * ssav.`attr_name` attr_name,
         * ssav.`attr_value`,
         * GROUP_CONCAT(DISTINCT info.`sku_id`) sku_ids
         * FROM `pms_sku_info` info
         * LEFT JOIN `pms_sku_sale_attr_value` ssav ON ssav.`sku_id`=info.`sku_id`
         * WHERE info.`spu_id`=3
         * GROUP BY ssav.`attr_id`,ssav.`attr_name`,ssav.`attr_value`;
         */
        List<SkuItemSaleAttrVo> skuItemSaleAttrVos=baseMapper.getSaleAttrBySpuId(spuId);

        return skuItemSaleAttrVos;
    }

    /**
     * 获取sku销售属性
     * @param skuId
     * @return
     */
    @Override
    public List<String> getSkuSaleAttrValuesAsStringList(Long skuId) {
        List<String> result=baseMapper.getSkuSaleAttrValuesAsStringList(skuId);
        return result;
    }

}